FUNCTION YBC_SWS_CHECK_FINISHED.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IV_WFCONFID) TYPE  YBC_SWS_WFCONFID
*"     VALUE(IV_APPLEVEL) TYPE  YBC_SWS_APPLEVEL
*"  EXPORTING
*"     VALUE(EV_FINISHED) TYPE  CHAR1
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"  TABLES
*"      IT_REQ_HEADER STRUCTURE  YBCS_SWS_FVALUE OPTIONAL
*"----------------------------------------------------------------------
  DATA:
    LS_TEMP     TYPE CHAR50,
    LT_TEMP     TYPE STANDARD TABLE OF CHAR50,
    LS_TAB      TYPE SWHACTOR,
    LT_TAB      TYPE STANDARD TABLE OF SWHACTOR,
    LS_WF_CND   TYPE YBC_SWS_WF_CND,
    LS_WF_01_T  TYPE YBC_SWS_WF_01,
    LT_WF_01    TYPE STANDARD TABLE OF YBC_SWS_WF_01,
    LT_WF_CND   TYPE STANDARD TABLE OF YBC_SWS_WF_CND,
    LT_WF_CND_T TYPE STANDARD TABLE OF YBC_SWS_WF_CND.

  DATA:
    LV_INDEX     TYPE I,
    LV_CONDITION TYPE STRING,
    LV_JSON      TYPE STRING,
    LT_SELOPT    TYPE RSDS_SELOPT_T,
    LV_CHAR20    TYPE CHAR20,
    LV_S_NAME    TYPE CHAR20,
    LV_ANDOR     TYPE YBC_SWS_WF_CND-ANDOR,
    S_FIELD1     TYPE RSDS_SELOPT_T,
    S_FIELD2     TYPE RSDS_SELOPT_T,
    S_FIELD3     TYPE RSDS_SELOPT_T,
    S_FIELD4     TYPE RSDS_SELOPT_T,
    S_FIELD5     TYPE RSDS_SELOPT_T,
    S_FIELD6     TYPE RSDS_SELOPT_T,
    S_FIELD7     TYPE RSDS_SELOPT_T,
    S_FIELD8     TYPE RSDS_SELOPT_T,
    S_FIELD9     TYPE RSDS_SELOPT_T,
    S_FIELD10    TYPE RSDS_SELOPT_T.

  FIELD-SYMBOLS <FT_RANGE> TYPE ANY TABLE.

** Dynamic Table Declarations
  DATA : LT_DYN_TABLE TYPE REF TO DATA,
         LS_DYN_LINE  TYPE REF TO DATA.

  FIELD-SYMBOLS:
    <FS_VALUE> TYPE ANY,
    <FS_LINE>  TYPE ANY,
    <FT_TABLE> TYPE STANDARD TABLE.

  DATA:
    LV_VALUE        TYPE STRING,
    LR_STRUCT_TYP   TYPE REF TO  CL_ABAP_DATADESCR,
    LR_DYNTABLE_TYP TYPE REF TO  CL_ABAP_TABLEDESCR,
    LS_COMPONENT    TYPE CL_ABAP_STRUCTDESCR=>COMPONENT,
    LT_COMPONENT    TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE.

  CLEAR EV_FINISHED.

  "Get config
  SELECT
    *
    INTO TABLE LT_WF_01
    FROM YBC_SWS_WF_01
   WHERE WFCONFID = IV_WFCONFID.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE S018(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_WFCONFID.
    RETURN.
  ENDIF.

  SORT LT_WF_01 BY APPLEVEL FINISH_CNDID.

  IF IV_APPLEVEL IS NOT INITIAL.
    DELETE LT_WF_01 WHERE APPLEVEL <> IV_APPLEVEL.
    IF LT_WF_01 IS INITIAL.
      ES_MSG-TYPE = 'E'.
      MESSAGE S017(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_WFCONFID IV_APPLEVEL.
      RETURN.
    ENDIF.
  ENDIF.

  "Get config condition
  SELECT *
    INTO TABLE LT_WF_CND
    FROM YBC_SWS_WF_CND
    FOR ALL ENTRIES IN LT_WF_01
   WHERE WFCONFID = LT_WF_01-WFCONFID
     AND WFCNDID = LT_WF_01-FINISH_CNDID.
  IF SY-SUBRC = 0.
    "动态创建内表
    LOOP AT IT_REQ_HEADER.
      CLEAR LS_COMPONENT.
      "字段中不能含有&,否则会Dump
      IF IT_REQ_HEADER-FIELD CS '&'.
        REPLACE ALL OCCURRENCES OF '&' IN IT_REQ_HEADER-FIELD WITH '_'.
      ENDIF.
      LS_COMPONENT-NAME = IT_REQ_HEADER-FIELD.

      READ TABLE LT_WF_CND TRANSPORTING NO FIELDS
        WITH KEY FIELD = IT_REQ_HEADER-FIELD FIELDTYPE = '01'."数字或者金额类型
      IF SY-SUBRC = 0.
        LS_COMPONENT-TYPE ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME( 'DEC_16_04_S' ).
      ELSE.
        LS_COMPONENT-TYPE ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME( 'TEXT1000' ).
      ENDIF.
      APPEND LS_COMPONENT TO LT_COMPONENT.
    ENDLOOP.

    IF LT_COMPONENT[] IS NOT INITIAL.
      LR_STRUCT_TYP  ?= CL_ABAP_STRUCTDESCR=>CREATE( P_COMPONENTS = LT_COMPONENT ).
      LR_DYNTABLE_TYP = CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE = LR_STRUCT_TYP ).

      CREATE DATA:
        LT_DYN_TABLE TYPE HANDLE LR_DYNTABLE_TYP,
        LS_DYN_LINE TYPE HANDLE LR_STRUCT_TYP.

      "生成动态内表
      ASSIGN LT_DYN_TABLE->* TO <FT_TABLE>.
      ASSIGN LS_DYN_LINE->* TO <FS_LINE>.

      LOOP AT IT_REQ_HEADER.
        ASSIGN COMPONENT IT_REQ_HEADER-FIELD OF STRUCTURE <FS_LINE> TO <FS_VALUE>.
        IF SY-SUBRC = 0.
          READ TABLE LT_WF_CND TRANSPORTING NO FIELDS
            WITH KEY FIELD = IT_REQ_HEADER-FIELD FIELDTYPE = '01'."数字或者金额类型
          IF SY-SUBRC = 0.
            REPLACE ALL OCCURRENCES OF ',' IN IT_REQ_HEADER-VALUE WITH ''.
          ENDIF.

          <FS_VALUE> = IT_REQ_HEADER-VALUE.
        ENDIF.
      ENDLOOP.

      IF <FS_LINE> IS NOT INITIAL.
        APPEND <FS_LINE> TO <FT_TABLE>.
      ENDIF.
    ENDIF.

    LOOP AT LT_WF_01 INTO LS_WF_01_T.
      IF LS_WF_01_T-FINISH_CNDID IS INITIAL. "优先取不带条件ID的
        CONTINUE.

      ELSE.
        LT_WF_CND_T = LT_WF_CND.
        DELETE LT_WF_CND_T WHERE WFCNDID <> LS_WF_01_T-FINISH_CNDID.
        "拼接条件字符串
        CLEAR:
          LV_CONDITION,
          LV_INDEX.
        LOOP AT LT_WF_CND_T INTO LS_WF_CND.
          LV_ANDOR = LS_WF_CND-ANDOR.
          IF LV_ANDOR IS INITIAL.
            LV_ANDOR = 'AND'.
          ENDIF.

          AT LAST.
            CLEAR LV_ANDOR.
          ENDAT.

          LV_JSON = LS_WF_CND-CNDS.
          /UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_JSON
                 CHANGING DATA = LT_SELOPT[] ).

          LV_INDEX = LV_INDEX + 1.
          IF LV_INDEX > 10.
            EXIT.
          ENDIF.

          LV_S_NAME = 'S_FIELD' && LV_INDEX.
          ASSIGN (LV_S_NAME) TO <FT_RANGE>.
          <FT_RANGE> = LT_SELOPT[].

          CONCATENATE LV_CONDITION LS_WF_CND-FIELD 'IN' LV_S_NAME LV_ANDOR INTO LV_CONDITION SEPARATED BY SPACE.
        ENDLOOP.

        "找到符合条件的审批配置
        LOOP AT <FT_TABLE> INTO <FS_LINE> WHERE (LV_CONDITION).
          EV_FINISHED = 'X'.
          EXIT.
        ENDLOOP.
        IF EV_FINISHED IS NOT INITIAL.
          EXIT.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDIF.
ENDFUNCTION.
